package com.windy.action.account;

import org.apache.commons.lang.StringUtils;

import com.windy.action.BaseAction;
import com.windy.constants.SessionConstant;
import com.windy.domain.account.User;
import com.windy.service.ServiceFactory;
import com.windy.service.interfaces.IAccountService;

public class LoginAction extends BaseAction {
	
	private static final long serialVersionUID = -5369239372878653678L;
	
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		if (StringUtils.isNotBlank(user.getLoginName())) {
			this.addActionError("用户名不能为空！");
			return ERROR;
		}
		
		if (StringUtils.isNotBlank(user.getPassword())) {
			this.addActionError("密码不能为空!");
			return ERROR;
		}
		
		IAccountService service = (IAccountService) ServiceFactory
		.getInstance().getService("accountServ");
		
		User existedUser = service.findUserByLoginName(user.getLoginName());
		if(existedUser == null)
		{
			this.addActionError("该用户名不存在，请重新输入！");
			return ERROR;
		}
		
		if(existedUser.getPassword().equals(user.getPassword()))
		{
			this.addActionError("密码不匹配，请重新输入！");
			return ERROR;
		}
		
		this.setSessionObject(SessionConstant.CUR_USER, existedUser);
		return SUCCESS;
	}
}
